package com.example.demo.Repository;


import com.example.demo.Entity.Item;
import com.example.demo.Entity.OperationLog;
import com.example.demo.Entity.Shelf;
import com.example.demo.Entity.User;
import com.example.demo.Entity.Warehouse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
@Repository
public interface OperationLogRepo extends JpaRepository<OperationLog, Long> {
    // 根据用户User查找操作日志
    List<OperationLog> findByUser(User user);

    // 根据操作类型查找操作日志
    List<OperationLog> findByIsRuku(boolean isRuku);

    // 根据操作时间查找操作日志
    List<OperationLog> findByDateTime(Date dateTime);

    // 根据仓库Warehouse查找操作日志
    List<OperationLog> findByWarehouse(Warehouse warehouse);

    // 根据货架Shelf查找操作日志
    List<OperationLog> findByShelf(Shelf shelf);

    // 根据商品Item查找操作日志
    List<OperationLog> findByItem(Item item);


    // 根据用户User和操作类型查找操作日志
    @Query("SELECT oplog FROM OperationLog oplog WHERE oplog.user = :user AND oplog.isRuku = :isRuku")
    List<OperationLog> findByUserAndType(@Param("user") User user, @Param("isRuku") boolean isRuku);

    // 根据操作时间范围查找操作日志
    @Query("SELECT oplog FROM OperationLog oplog WHERE oplog.dateTime BETWEEN :startDate AND :endDate")
    List<OperationLog> findByDateTimeBetween(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Query("SELECT o FROM OperationLog o WHERE o.item.id = :itemId AND o.isRuku = false AND o.dateTime BETWEEN :oneWeekAgo AND :now")
    List<OperationLog> findOutgoingLogsForItemBetweenDates(@Param("itemId") Long itemId, @Param("oneWeekAgo") Date oneWeekAgo, @Param("now") Date now);

    // 可以根据需求添加更多的查询方法
}